package com.qf.controller;

import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletInputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Scanner;

@RestController
@RequestMapping("/server2")
public class Server1Controller {

    @Value("${server.port}")
    private String port;


    @CrossOrigin("*") // 所有的请求都可以访问这个接口
    @RequestMapping("/hello")
    public String hello(HttpServletRequest request, @RequestBody String callback, HttpServletResponse response) throws IOException {


        ServletInputStream inputStream = request.getInputStream();

        Scanner scanner = new Scanner(inputStream);
        while(scanner.hasNext()){
            System.out.println(scanner.nextLine());
        }


        // 1.创建一个cookie
        Cookie cookie = new Cookie("server-2", "server-2-value");

        // 2.设置cookie的属性
        cookie.setMaxAge(60 * 60 * 24 * 7);
        cookie.setHttpOnly(false); // 可以通过js访问cookie
        cookie.setSecure(false); // 走http的协议
//        cookie.setValue(); // 覆盖的上面的value值
        cookie.setPath("/"); // 不设置，默认是当前路径


        // 3.把cookie写入浏览器中
        response.addCookie(cookie);


        return callback +  "('"+ "hello:"+port +"')";
    }
}
